package com.baosu.controller.back;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baosu.domain.Category;
import com.baosu.domain.Result;
import com.baosu.domain.Tag;
import com.baosu.mapper.CategoryMapper;
import com.baosu.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "分类接口-后台")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping
    @ApiOperation(value = "分类下拉框")
    public Result getAllCategory(){
        List<Category> categorys = categoryService.list();
        return Result.ok(categorys);
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页列表,模糊搜索")
    public Result<Page> list(int page, int pageSize, String name){
        return categoryService.pageTagList(page,pageSize,name);
    }

    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result add(@RequestBody Category category){
        categoryService.save(category);
        return Result.ok(null).message("新增分类成功");
    }

    @DeleteMapping
    @ApiOperation(value = "删除分类")
    public Result delete(@RequestParam List<Long> ids){
        return categoryService.deleteCategory(ids);
    }

    @PutMapping
    @ApiOperation(value = "修改分类")
    public Result edit(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok(null).message("修改成功");
    }


    @GetMapping("/articleList")
    @ApiOperation(value = "根据分类ID查询文章列表")
    public Result getArticlelistByCategoryId(int pagenum, int pagesize, Long id){
        return categoryService.getArticlelistByCategoryId(pagenum,pagesize,id);
    }


}
